﻿/**
Library: Alternative Transitioning
Author: Valentyn Derkach 2007.07.04
———————————————————————————————————————————————————————————————————————————————————————————————————————————————————
TweenProcessorSpeeding performs a speeding tween processing. It uses a change factor to calculate 
a new value based on the current and final values. The change factor tends from its initial value to the
destination change factor's value using speedFactor as a change factor for its tending.
Equations are:
	changeFactor = (endChangeFactor - changeFactor)*speedFactor;
	newValue = currentValue + changeFactor*(endValue - currentValue)
/**/

package com.bizmlib.transitioning
{

	import com.bizmlib.transitioning.ITweenProcessor;


	public class TweenProcessorSpeeding extends Object implements ITweenProcessor
	{

		public var initSpeed:Number = 0;
		public var curSpeed:Number = 0;
		public var dstSpeed:Number = 0;
		public var speedFactor:Number = 0;
		public var minLimit:Number = 1;
		public var maxLimit:Number = Number.POSITIVE_INFINITY;
		private var maxDValue:Number = 0;


		public function TweenProcessorSpeeding(initSpeed:Number, endSpeed:Number, speedChangeFactor:Number, minLimit:Number, maxLimit:Number){
			if(!isNaN(initSpeed)){this.initSpeed = curSpeed = initSpeed;}
			if(!isNaN(endSpeed)){dstSpeed = endSpeed;}
			if(!isNaN(speedChangeFactor)){speedFactor = speedChangeFactor;}
			if(!isNaN(minLimit)){this.minLimit = minLimit;}
			if(!isNaN(maxLimit)){this.maxLimit = maxLimit;}
		}

		public function reInit():void{curSpeed = initSpeed;}

		public function onBeforeIteration():void{
			maxDValue = 0;
			curSpeed += (dstSpeed - curSpeed)*speedFactor;
		}

		public function performCalculation(propertyName:String, currentValue:Number, endValue:Number):Number{
			//trace(this + ".performCalculation(" + arguments + ")");
			var newValue:Number = Math.abs(endValue-currentValue)<=minLimit? endValue : currentValue + (endValue>currentValue?1:-1)*Math.min(maxLimit, Math.abs((endValue - currentValue)*curSpeed));
			maxDValue = Math.max(maxDValue, Math.abs(newValue - endValue));
			return newValue;
		}

		public function isFinal():Boolean
		{
			if(maxDValue == 0){return true;}
			return false;
		}

		public function onAfterIteration():void{
		}

		public function toString():String
		{
			return "[com.bizmlib.transitioning.TweenProcessorSpeeding]";
		}
	}
}